﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.IO;
using WebVideo.Areas.Admin.Models;
using CarlosAg.ExcelXmlWriter;
using WebVideo.Controllers;

namespace WebVideo.Areas.Admin.Controllers
{
    public class ReportPointController : Controller
    {
        //
        // GET: /Admin/ReportPoint/

        public ActionResult Index()
        {
            return View(ReportPointModel.Index());
        }
        #region "Delete"
        public string Delete(int Id)
        {
            try
            {
                ReportPointModel.Delete(Id);
                return "ok";
            }
            catch
            {
                return "error";
            }
        }
        #endregion //Delete

        #region "doitrangthai"
        public ActionResult Ban(int Id, int l,string mail,int sotien, string taikhoan, string name, string nganhang)
        {
            try
            {
                ReportPointModel.Ban(Id, l);
                if (mail != "" && mail !=null)
                {
                    return RedirectToAction("Doidiem", "../Mailer", new { mail = mail, sotien=sotien , taikhoan = taikhoan, name= name ,nganhang=nganhang});
                }
                else if (mail == "" && mail ==null)
                {
                    return RedirectToAction("Index", "ReportPoint");
                }
                return RedirectToAction("Index", "ReportPoint");
            }
            catch
            {
                return RedirectToAction("Index", "Error", new { errorMsg = "Lỗi SQL hosting / server" });
            }
        }
        #endregion
        #region "Insert"
        public ActionResult Insert()
        {
            ReportPointModel rp = new ReportPointModel();
            return View(rp);
        }

        [ValidateInput(false)]
        [HttpPost]
        public ActionResult Insert(ReportPointModel RP)
        {

            try
            {
                if (ModelState.IsValid)
                {
                    ReportPointModel.Insert(RP);
                    return RedirectToAction("Index", "ReportPoint");
                }
            }
            catch
            {

                return RedirectToAction("Index", "Error", new { errorMsg = "Lỗi SQL hosting / server" });
            }
            return View(RP);
        }
        #endregion //insert

        #region "Edit"
        public ActionResult Edit(int IdFAQ)
        {


            return View(FAQModel.LayModel(IdFAQ));

        }

        [ValidateInput(false)]
        [HttpPost]
        public ActionResult Edit(FAQModel FAQ)
        {

            try
            {
                if (ModelState.IsValid)
                {
                    FAQModel.Edit(FAQ);
                    return RedirectToAction("Index", "FAQ");
                }
            }
            catch
            {

                return RedirectToAction("Index", "Error", new { errorMsg = "Lỗi SQL hosting / server" });
            }
            return View(FAQ);
        }
        #endregion //Edit

        #region "Xuat bao cao"
        public JsonResult saveExcel()
        {
            Workbook book = new Workbook();
            ReportController rp = new ReportController();
            IList<Report> TieuDe = new List<Report>();
            IList<Report> NoiDung = new List<Report>();
            string Name = "Danh sách báo cáo đổi điểm thành tiền";
            dbVideoDataContext db = new dbVideoDataContext();
            // tạo danh sách tiêu đề
            var query = db.EntityReport_Points.OrderByDescending(g=>g.Date).ToList();

            Report k = new Report();
            k.Name = "UserName";
            TieuDe.Add(k);

            k = new Report();
            k.Name = "Tình trạng";
            TieuDe.Add(k);

            k = new Report();
            k.Name = "Số tài khoản";
            TieuDe.Add(k);

            k = new Report();
            k.Name = "Tên ngân hàng";
            TieuDe.Add(k);

            k = new Report();
            k.Name = "Số tiền đổi";
            TieuDe.Add(k);

            k = new Report();
            k.Name = "Ngày đổi";
            TieuDe.Add(k);

            foreach (var item in query)
            {
                k = new Report();
                k.Name = item.UserName;
                NoiDung.Add(k);

                k = new Report();
                if (item.Status == 1)
                    k.Name = "Đã đổi";
                else
                    k.Name = "Chưa đổi";
                NoiDung.Add(k);

                k = new Report();
                k.Name = item.EntityUser.BankAccount;
                NoiDung.Add(k);

                k = new Report();
                k.Name = item.EntityUser.NameBank;
                NoiDung.Add(k);

                k = new Report();
                k.Name = item.Pointed.ToString() + " K";
                NoiDung.Add(k);

                k = new Report();
                k.Name = item.Date.ToString();
                NoiDung.Add(k);
            }

            book = rp.Index(book, TieuDe, NoiDung, Name);
            string name = "BaoCao_DoiDiem_YeuQuangCao" + ".xls";
            var path = Path.Combine(Server.MapPath("~/Content/"), name);
            book.Save(path);
            return Json(name);
        }

        #endregion

    }
}
